# ai_sop_system/data_importer.py
import pandas as pd
from datetime import datetime
from typing import Dict, List, Any


class DataImporter:
    """数据导入模块"""

    def __init__(self):
        self.import_history = []
        self.data_validator = DataValidator()

    def import_data(self, user_data: Dict[str, Any]) -> Dict[str, Any]:
        """导入用户数据到系统"""
        print("📥 开始导入用户数据...")

        # 数据验证
        validation_result = self.data_validator.validate_user_data(user_data)
        if not validation_result["is_valid"]:
            raise ValueError(f"数据验证失败: {validation_result['errors']}")

        # 数据转换
        transformed_data = self._transform_data(user_data)

        # 数据增强
        enhanced_data = self._enhance_data(transformed_data)

        # 记录导入历史
        import_record = {
            "timestamp": datetime.now(),
            "data_id": user_data.get("session_id"),
            "data_size": len(str(user_data)),
            "validation_score": validation_result["score"],
            "status": "success"
        }
        self.import_history.append(import_record)

        print("✅ 用户数据导入完成")
        return enhanced_data

    def _transform_data(self, user_data: Dict) -> Dict:
        """数据转换"""
        transformed = user_data.copy()

        # 标准化数据结构
        transformed["standardized_data"] = {
            "user_profile": self._extract_user_profile(user_data),
            "behavior_patterns": self._extract_behavior_patterns(user_data),
            "preference_summary": self._extract_preference_summary(user_data),
            "context_summary": self._extract_context_summary(user_data)
        }

        return transformed

    def _enhance_data(self, data: Dict) -> Dict:
        """数据增强"""
        enhanced = data.copy()

        # 添加衍生特征
        enhanced["derived_features"] = {
            "user_segment": self._calculate_user_segment(data),
            "engagement_level": self._calculate_engagement_level(data),
            "complexity_tolerance": self._estimate_complexity_tolerance(data),
            "learning_capacity": self._estimate_learning_capacity(data)
        }

        # 添加预测指标
        enhanced["predictive_metrics"] = {
            "sop_success_probability": self._predict_sop_success(data),
            "preferred_communication_channel": self._predict_communication_channel(data),
            "estimated_attention_span": self._estimate_attention_span(data)
        }

        return enhanced


class DataValidator:
    """数据验证器"""

    def validate_user_data(self, user_data: Dict) -> Dict[str, Any]:
        """验证用户数据"""
        errors = []
        warnings = []

        # 检查必需字段
        required_fields = ["timestamp", "session_id", "data_types"]
        for field in required_fields:
            if field not in user_data:
                errors.append(f"缺少必需字段: {field}")

        # 检查数据完整性
        if "data_types" in user_data:
            data_types = user_data["data_types"]
            if not data_types.get("behavior") or not data_types.get("preference"):
                warnings.append("部分数据类型数据不完整")

        # 计算验证分数
        score = self._calculate_validation_score(user_data, errors, warnings)

        return {
            "is_valid": len(errors) == 0,
            "score": score,
            "errors": errors,
            "warnings": warnings
        }

    def _calculate_validation_score(self, user_data: Dict, errors: List, warnings: List) -> float:
        """计算验证分数"""
        base_score = 1.0

        # 错误扣分
        base_score -= len(errors) * 0.3

        # 警告扣分
        base_score -= len(warnings) * 0.1

        # 数据完整性加分
        if self._check_data_completeness(user_data):
            base_score += 0.2

        return max(0.0, min(1.0, base_score))

    def _check_data_completeness(self, user_data: Dict) -> bool:
        """检查数据完整性"""
        try:
            data_types = user_data.get("data_types", {})
            return all([
                data_types.get("behavior"),
                data_types.get("preference"),
                data_types.get("context"),
                data_types.get("feedback")
            ])
        except:
            return False